package com.smartao.base.role;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.smartao.base.Constant;
import com.smartao.base.init.InitializeServiceOrder;
import com.smartao.base.selectitem.Selectitem;
import com.smartao.base.selectitem.SelectitemService;
import com.smartao.core.init.AbstractDataInitializeService;

@Service
public class RoleInitializeService extends AbstractDataInitializeService {
	
	private static final String ADMIN_NAME = "系统管理员";
	//角色创建在用户之前
	protected final int order = InitializeServiceOrder.INIT_ORDER_ROLE;
	
	@Autowired
	private RoleService roleService;

	@Autowired
	private SelectitemService selectitemService;
	@Override
	public int getOrder() {
		return order;
	}
	
	@Override
	public void initData() throws Exception {
		Role role = roleService.findByRoleKey(Constant.ROLE_ADMIN);
		
		if (role == null) {
			logger.info("Role administrators not found,will create one.");
			Selectitem roleTypeSystem = selectitemService.findByCode(Constant.SELECTITEM_ROLE_TYPE_SYSTEM);
			Role admin = new Role();
			admin.setRoleKey(Constant.ROLE_ADMIN);
			admin.setRoleType(roleTypeSystem);
			admin.setName(ADMIN_NAME);
			admin.setDescription("系统管理员，系统内置角色");
			roleService.save(admin);
		}
	}

}
